﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

using Codeology.Auctioneer;

namespace Codeology.Auctioneer.UI
{

    public partial class MainForm : Form
    {

        private AuctioneerScanData scan_data = null;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            tabControl.TabIndex = 0;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (openDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;

            // Get current cursor
            Cursor stored_cursor = Cursor.Current;

            try {
                // Switch cursor
                Cursor.Current = Cursors.WaitCursor;

                // Open file
                FileStream file = new FileStream(openDialog.FileName,FileMode.Open,FileAccess.Read,FileShare.Read);

                try {
                    // Load and parse scan data
                    scan_data = AuctioneerScanData.Load(file);

                    // Clear
                    cbxRealm.Items.Clear();
                    cbxFaction.Items.Clear();
                    listView.Items.Clear();
                    lblCount.Text = "Auction Count: 0";

                    // Add realms
                    foreach(AuctioneerRealm realm in scan_data.Realms) cbxRealm.Items.Add(realm);

                    // Select if there are items
                    if (cbxRealm.Items.Count > 0) cbxRealm.SelectedIndex = 0;
                } finally {
                    // Close file
                    file.Close();
                }
            } finally {
                // Restore cursor
                Cursor.Current = stored_cursor;
            }
        }

        private void cbxRealm_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get current cursor
            Cursor stored_cursor = Cursor.Current;

            try {
                // Switch cursor
                Cursor.Current = Cursors.WaitCursor;

                // Clear
                cbxFaction.Items.Clear();
                listView.Items.Clear();
                lblCount.Text = "Auction Count: 0";

                // Check for selection
                if (cbxRealm.SelectedIndex < 0) return;

                // Get realm
                AuctioneerRealm realm = (AuctioneerRealm)cbxRealm.Items[cbxRealm.SelectedIndex];

                // Add factions
                foreach(AuctioneerFaction faction in realm.Factions) cbxFaction.Items.Add(faction);

                // Select if there are items
                if (cbxFaction.Items.Count > 0) cbxFaction.SelectedIndex = 0;
            } finally {
                // Restore cursor
                Cursor.Current = stored_cursor;
            }
        }

        private void cbxFaction_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get current cursor
            Cursor stored_cursor = Cursor.Current;

            try {
                // Switch cursor
                Cursor.Current = Cursors.WaitCursor;

                // Clear
                listView.Items.Clear();

                // Check for selection
                if (cbxFaction.SelectedIndex < 0) return;

                // Get faction
                AuctioneerFaction faction = (AuctioneerFaction)cbxFaction.Items[cbxFaction.SelectedIndex];

                // Process items
                listView.BeginUpdate();

                try {
                    foreach(AuctioneerItem auction_item in faction.AuctionItems) {
                        // Create list item
                        ListViewItem item = listView.Items.Add(auction_item.Quality.ToString());

                        // Set up list item
                        item.Tag = auction_item;

                        item.SubItems.Add(auction_item.Name);
                        item.SubItems.Add(auction_item.Type);
                        item.SubItems.Add(auction_item.SubType);
                        item.SubItems.Add(auction_item.Price.ToString());
                        item.SubItems.Add(auction_item.Buyout.ToString());
                        item.SubItems.Add(auction_item.Seller);
                        item.SubItems.Add(String.Empty);
                    }
                } finally {
                    listView.EndUpdate();
                }

                // Set count
                lblCount.Text = "Auction Count: " + String.Format("{0:n0}",listView.Items.Count);
            } finally {
                // Restore cursor
                Cursor.Current = stored_cursor;
            }
        }

        private void listView_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            // Get auction item
            AuctioneerItem auction_item = (AuctioneerItem)e.Item.Tag;

            if (e.ColumnIndex == 0) {
                // Set color
                Color color;

                switch (auction_item.Quality) {
                    case AuctioneerItemQuality.Poor:        color = Color.Gray;
                                                            break;
                    case AuctioneerItemQuality.Common:      color = Color.Silver;
                                                            break;
                    case AuctioneerItemQuality.Uncommon:    color = Color.LimeGreen;
                                                            break;
                    case AuctioneerItemQuality.Rare:        color = Color.Blue;
                                                            break;
                    case AuctioneerItemQuality.Epic:        color = Color.Purple;
                                                            break;
                    case AuctioneerItemQuality.Legendary:   color = Color.Orange;
                                                            break;
                    case AuctioneerItemQuality.Artifact:    color = Color.Orchid;
                                                            break;
                    default:                                color = listView.BackColor;
                                                            break;
                }

                // Fill in a small block representing the color of the item
                Rectangle rect = new Rectangle(e.Bounds.Left + 2,e.Bounds.Top + 2,e.Bounds.Width - 4,e.Bounds.Height - 4);
                SolidBrush brush = new SolidBrush(color);

                e.Graphics.FillRectangle(brush,rect);
            } else {
                e.DrawDefault = true;
            }
        }

        private void listView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            // Default drawing
            e.DrawDefault = true;
        }

    }

}
